﻿using Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml;

/// <summary>
/// sitemap 的摘要说明
/// </summary>
public class sitemap
{
    public sitemap()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }


    /// <summary>
    /// 生成百度站点地图
    /// </summary>
    /// <returns></returns>
    public void GetBaiDuSiteMap()
    {
        XmlDocument xmlDoc = new XmlDocument();
        //获取新闻站点地图的本地保存路径
        string path = System.Web.HttpContext.Current.Server.MapPath("/Sitemap.xml");
        xmlDoc.Load(path);
        //查找根节点
        XmlNode root = xmlDoc.SelectSingleNode("urlset");
        //删除以前的所有老节点
        root.RemoveAll();
        #region 平台公告
        //查询最新100条平台公告

        Entities mydb = new Entities();
  

        var  platformnotice = mydb.L_Newss.Where(x=>x.IsLock!=null).OrderByDescending(x=>x.Time);
      
            foreach (var platformNoticeNews in platformnotice)
            {
                XmlElement xesub1 = xmlDoc.CreateElement("url");
                XmlElement xe1 = xmlDoc.CreateElement("loc");
                xe1.InnerText = HttpContext.Current.Request.Url.Host.ToString() + "/pages_" + platformNoticeNews.ClassId + "_" + platformNoticeNews.NID + ".aspx";
                XmlElement xe2 = xmlDoc.CreateElement("lastmod");
                xe2.InnerText = Convert.ToDateTime(platformNoticeNews.Time).ToString("yyyy-MM-dd");
                XmlElement xe3 = xmlDoc.CreateElement("changefreq");
                xe3.InnerText = "daily";
                XmlElement xe4 = xmlDoc.CreateElement("priority");
                xe4.InnerText = "0.2";
                xesub1.AppendChild(xe1);
                xesub1.AppendChild(xe2);
                xesub1.AppendChild(xe3);
                xesub1.AppendChild(xe4);
                root.AppendChild(xesub1);
            }
      
        #endregion
        xmlDoc.AppendChild(root);
        xmlDoc.Save(path);
    }
}